package com.yswee.mallnavi.view.navi;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.yswee.framework.data.BaseArrayData;
import com.yswee.framework.data.parser.IParser;
import com.yswee.framework.net.ITaskContext;
import com.yswee.framework.net.http.Callback;
import com.yswee.framework.net.http.Entity;
import com.yswee.framework.utils.StringUtils;
import com.yswee.framework.utils.ViewInject;
import com.yswee.framework.widget.MToast;
import com.yswee.framework.widget.clickedtab.ClickedTabView;
import com.yswee.framework.widget.loading.LoadingLayout;
import com.yswee.mallnavi.R;
import com.yswee.mallnavi.entity.info.BrandEntity;
import com.yswee.mallnavi.entity.info.CategoryEntity;
import com.yswee.mallnavi.model.info.BrandModel;
import com.yswee.mallnavi.model.info.NaviModel;
import com.yswee.mallnavi.view.brand.InfoView;

import java.util.ArrayList;

public class NaviView extends LoadingLayout {

    private ContentView mContentView;

    private NaviModel mNaviModel;
    private BrandModel mBrandModel;
    private IFloorChanged mFloorChanged;

    public void setFloorChanged(IFloorChanged listener) {
        mFloorChanged = listener;
    }

    public NaviView(Context context) {
        super(context);
    }

    public NaviView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected View getLayoutView() {
        mContentView = new ContentView(mContext);
        return mContentView;
    }

    @Override
    protected void onBindListener() {
        super.onBindListener();
    }

    @Override
    protected void onApplyData() {
        super.onApplyData();
    }

    @Override
    public void onApplyLoadingData() {
        getDataInfo();
    }

    private void getDataInfo() {
        if(mNaviModel == null) {
            ITaskContext context = mContext instanceof ITaskContext ?
                    (ITaskContext) mContext : null;
            mNaviModel = new NaviModel(context);
        }
        gotoLoading();
        mNaviModel.getCategoryList(new Callback<BaseArrayData<CategoryEntity>>() {
            @Override
            public void onFinish(Entity<BaseArrayData<CategoryEntity>> entity) {
                IParser<BaseArrayData<CategoryEntity>> parser = entity.getParser();
                final BaseArrayData<CategoryEntity> data = entity.getParsedData();
                if(parser.getIsOk() && data != null && data.getArray() != null) {
                    applyDataInfo(data.getArray());
                    gotoSuccessful();
                    return;
                }
                String msg = entity.getMessage();
                if(StringUtils.isEmpty(msg)) {
                    msg = getResources().getString(R.string.navi_getdata_fail);
                }
                MToast.showToastMessage(msg);
                gotoError();
            }

            @Override
            public void onError(Entity<BaseArrayData<CategoryEntity>> entity) {
                gotoError();
                if(entity.getEntityStatus() != Entity.NETINVALID) {
                    MToast.showToastMessage(R.string.navi_getdata_fail);
                }
            }
        });
    }

    private void applyDataInfo(ArrayList<CategoryEntity> array) {
        mContentView.setDataSource(array);
    }

    private void getBrandInfo(String brandId) {
        if(mBrandModel == null) {
            ITaskContext context = mContext instanceof ITaskContext ?
                    (ITaskContext) mContext : null;
            mBrandModel = new BrandModel(context);
        }
        gotoLoading();
        mBrandModel.getInfo(brandId, new Callback<BrandEntity>() {
            @Override
            public void onFinish(Entity<BrandEntity> entity) {
                IParser<BrandEntity> parser = entity.getParser();
                final BrandEntity data = entity.getParsedData();
                if(parser.getIsOk() && data != null) {
                    mContentView.showBrandInfo(data);
                    gotoSuccessful();
                    return;
                }
                String msg = entity.getMessage();
                if(StringUtils.isEmpty(msg)) {
                    msg = getResources().getString(R.string.brand_getdata_fail);
                }
                MToast.showToastMessage(msg);
                gotoError();
            }

            @Override
            public void onError(Entity<BrandEntity> entity) {
                gotoError();
                if(entity.getEntityStatus() != Entity.NETINVALID) {
                    MToast.showToastMessage(R.string.brand_getdata_fail);
                }
            }
        });
    }

    private class ContentView extends ClickedTabView {

        @ViewInject
        private LinearLayout lltitle;
        @ViewInject
        private FrameLayout uvcontainer;
        @ViewInject
        private TabBar uvtabbar;
        @ViewInject
        private InfoView uvinfo;

        private ArrayList<CategoryEntity> mDataArray;
        private ItemView[] mViews;

        public void setDataSource(ArrayList<CategoryEntity> array) {
            mDataArray = array;
            uvtabbar.setDataArray(array);
            mViews = new ItemView[array.size()];
            onApplyData();
        }

        @Override
        protected boolean autoLoadData() {
            return false;
        }

        public ContentView(Context context) {
            super(context);
        }

        public ContentView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        @Override
        protected int getLayoutResId() {
            return R.layout.view_info_navi;
        }

        @Override
        protected int getTabBarResID() {
            return R.id.uvtabbar;
        }

        @Override
        protected int getTabContainerResID() {
            return R.id.uvcontainer;
        }

        @Override
        protected int getTabCount() {
            return mDataArray.size();
        }

        @Override
        protected View getTabView(int index) {
            if (mDataArray != null && index < mDataArray.size()) {
                if (mViews[index] == null) {
                    mViews[index] = new ItemView(mContext);
                    mViews[index].setDataSource(mDataArray.get(index));
                    mViews[index].setActionCallback(new ItemView.IActionCallback() {
                        @Override
                        public void onBrandClicked(String brandId) {
                            NaviView.this.getBrandInfo(brandId);
                        }
                    });
                }
                return mViews[index];
            }
            return new View(mContext);
        }

        @Override
        protected void onBindListener() {
            super.onBindListener();
            uvinfo.setActionCallback(new InfoView.IActionCallback() {
                @Override
                public void onBackClicked(View v) {
                    uvinfo.setVisibility(View.GONE);
                    lltitle.setVisibility(View.VISIBLE);
                    uvcontainer.setVisibility(View.VISIBLE);
                    uvtabbar.setVisibility(View.VISIBLE);
                }
            });
        }

        @Override
        protected void onApplyData() {
            super.onApplyData();
        }

        @Override
        public void onTabChanged(int index) {
            super.onTabChanged(index);
            if (NaviView.this.mFloorChanged != null) {
                NaviView.this.mFloorChanged.onFloorChanged(mDataArray.get(index));
            }
        }

        private void showBrandInfo(BrandEntity brand) {
            lltitle.setVisibility(View.GONE);
            uvcontainer.setVisibility(View.GONE);
            uvtabbar.setVisibility(View.GONE);
            uvinfo.setDataSource(brand);
            uvinfo.setVisibility(View.VISIBLE);
        }
    }

    public interface IFloorChanged {
        void onFloorChanged(CategoryEntity categoryEntity);
    }
}
